package com.mh.service;

import com.mh.dao.ItemDAO;
import com.mh.dao.ProductDAO;

import com.mh.domain.Item;
import com.mh.domain.Product;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

/**
 * Spring service that handles CRUD requests for Product entities
 * 
 */

@Service("ProductService")
@Transactional
public class ProductServiceImpl implements ProductService {

	/**
	 * DAO injected by Spring that manages Item entities
	 * 
	 */
	@Autowired
	private ItemDAO itemDAO;

	/**
	 * DAO injected by Spring that manages Product entities
	 * 
	 */
	@Autowired
	private ProductDAO productDAO;

	/**
	 * Instantiates a new ProductServiceImpl.
	 *
	 */
	public ProductServiceImpl() {
	}

	/**
	 * Return all Product entity
	 * 
	 */
	@Transactional
	public List<Product> findAllProducts(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<Product>(productDAO.findAllProducts(startResult, maxRows));
	}

	/**
	 * Delete an existing Item entity
	 * 
	 */
	@Transactional
	public Product deleteProductItems(Integer product_id, Integer related_items_id) {
		Item related_items = itemDAO.findItemByPrimaryKey(related_items_id, -1, -1);

		Product product = productDAO.findProductByPrimaryKey(product_id, -1, -1);

		related_items.setProduct(null);
		product.getItems().remove(related_items);

		itemDAO.remove(related_items);
		itemDAO.flush();

		return product;
	}

	/**
	 * Return a count of all Product entity
	 * 
	 */
	@Transactional
	public Integer countProducts() {
		return ((Long) productDAO.createQuerySingleResult("select count(o) from Product o").getSingleResult()).intValue();
	}

	/**
	 * Delete an existing Product entity
	 * 
	 */
	@Transactional
	public void deleteProduct(Product product) {
		productDAO.remove(product);
		productDAO.flush();
	}

	/**
	 * Save an existing Product entity
	 * 
	 */
	@Transactional
	public void saveProduct(Product product) {
		Product existingProduct = productDAO.findProductByPrimaryKey(product.getId());

		if (existingProduct != null) {
			if (existingProduct != product) {
				existingProduct.setId(product.getId());
				existingProduct.setName(product.getName());
				existingProduct.setPrice(product.getPrice());
				existingProduct.setCreatedBy(product.getCreatedBy());
				existingProduct.setCreatedOn(product.getCreatedOn());
				existingProduct.setModifiedBy(product.getModifiedBy());
				existingProduct.setModifiedOn(product.getModifiedOn());
			}
			product = productDAO.store(existingProduct);
		} else {
			product = productDAO.store(product);
		}
		productDAO.flush();
	}

	/**
	 * Load an existing Product entity
	 * 
	 */
	@Transactional
	public Set<Product> loadProducts() {
		return productDAO.findAllProducts();
	}

	/**
	 * Save an existing Item entity
	 * 
	 */
	@Transactional
	public Product saveProductItems(Integer id, Item related_items) {
		Product product = productDAO.findProductByPrimaryKey(id, -1, -1);
		Item existingitems = itemDAO.findItemByPrimaryKey(related_items.getId());

		// copy into the existing record to preserve existing relationships
		if (existingitems != null) {
			existingitems.setId(related_items.getId());
			existingitems.setQuantity(related_items.getQuantity());
			existingitems.setCost(related_items.getCost());
			existingitems.setCreatedBy(related_items.getCreatedBy());
			existingitems.setCreatedOn(related_items.getCreatedOn());
			existingitems.setModifiedBy(related_items.getModifiedBy());
			existingitems.setModifiedOn(related_items.getModifiedOn());
			related_items = existingitems;
		}

		related_items.setProduct(product);
		product.getItems().add(related_items);
		related_items = itemDAO.store(related_items);
		itemDAO.flush();

		product = productDAO.store(product);
		productDAO.flush();

		return product;
	}

	/**
	 */
	@Transactional
	public Product findProductByPrimaryKey(Integer id) {
		return productDAO.findProductByPrimaryKey(id);
	}

	public List<Product> findAllProducts(Product product, Integer startResult,
			Integer maxRows) {
		return productDAO.findAllProducts(product, startResult, maxRows);
	}
	
	/**
	 */
	@Transactional
	public Set<Product>  findProducts(Product product) {
		return productDAO.findProducts(product);
	}
	
	
}
